package com.module.organization;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import base.util.Page;

import com.module.organization.manager.OrganizationManager;
import com.module.organization.vo.EntBind;
import com.module.organization.vo.Organization;
import com.webClient.client.SocketClient;
import com.webClient.service.WebClientService;

public class OrganizationService
{
    static WebClientService ss = null;
    static SocketClient sClient = null;
    static Logger logger = Logger.getLogger(OrganizationService.class.getName ()) ;
    
    
    public Page tolist(Organization organization, final Integer start, final Integer limit)
        throws Exception
    {
        Page page = new Page();
        page.setStart(start);
        page.setLimit(limit);
        OrganizationManager organizationManager = OrganizationManager.getInstance();
        return organizationManager.tolist(organization,page);
    }

    
    public Map save(Organization organization) throws Exception{
    	OrganizationManager organizationManager=OrganizationManager.getInstance();
    	boolean isTrue=false;
        if(organization.getId()!=null){
        	isTrue=organizationManager.update(organization);
        }else{
        	isTrue=organizationManager.add(organization);
        }
        Map<String, Object> statusMap = new HashMap<String, Object>();
		statusMap.put("state", isTrue);
		if (isTrue == true) {
			statusMap.put("msg", "保存成功");
		} else {
			statusMap.put("msg", "保存失败");
		}
		return statusMap;
    }
    /**
     * 绑定企业应用
     * @param entbind
     * @return
     * @throws Exception
     */
    
    public Map saveEntBind(EntBind entbind) throws Exception{
    	 OrganizationManager organizationManager=OrganizationManager.getInstance();
    	 boolean isTrue=organizationManager.saveEntBind(entbind);
    	 Map<String, Object> statusMap = new HashMap<String, Object>();
 		 statusMap.put("state", isTrue);
 		 if (isTrue == true) {
 			statusMap.put("msg", "保存成功");
 		 } else {
 			statusMap.put("msg", "保存失败");
 		 }
 		 return statusMap;
    }
    
    /**
     * 获取企业应用
     * @param entid
     * @return
     * @throws Exception
     */
    
    
    public List getOrgApp(String entid) throws Exception{
         OrganizationManager  organizationManager=OrganizationManager.getInstance();
         List list=organizationManager.getOrgApp(entid);
         return list;
    }
    
    
    /**
     * 根据登陆用户查找所属企业的所有子企业
     * @param orgid 用户所属企业id
     * @return
     * @throws Exception
     */
    public List findOrgByPId(String orgid) throws Exception{
    	 OrganizationManager organizationManager=OrganizationManager.getInstance();
    	 List list=organizationManager.findOrgByPId(orgid);
         return list;
    }
    public Organization findById(String id) throws Exception{
        OrganizationManager organizationManager=OrganizationManager.getInstance();
        Organization organization = organizationManager.findById(id);
        return organization;
    }
    
    public Map remove(ArrayList<String> idList) throws Exception{
        String[] ids = new String[idList.size()];
        if(idList.size()>0){
            for(int i=0;i<idList.size();i++){
                ids[i]="'"+idList.get(i)+"'";
             }
        }
        Organization organization = new Organization();
        organization.setIds(ids);
        OrganizationManager organizationManager=OrganizationManager.getInstance();
        boolean isTrue = organizationManager.delete(ids);
        Map<String, Object> statusMap = new HashMap<String, Object>();
		statusMap.put("state", isTrue);
		if (isTrue == true) {
			statusMap.put("msg", "删除成功");
		} else {
			statusMap.put("msg", "删除失败");
		}
		return statusMap;
    }
  
    /**
     * 获取传入的父结点的树结构
     * @param pid
     * @return
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    public List getOrganizationTree(String pid) throws Exception{
        Organization organization = new Organization(); 
        organization.setPid(pid);
        OrganizationManager organizationManager=OrganizationManager.getInstance();
        List list=organizationManager.getOrganizationTree(organization);
        return list;
    }
    
    /**
     * 获取以自己ID为父结点的树结构
     * @param id
     * @return
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    public List<Organization> getSelfTree(String id) throws Exception{ 
        Organization organization = new Organization(); 
        organization.setId(id);
        OrganizationManager organizationManager=OrganizationManager.getInstance();
        List<Organization> list=organizationManager.getSelfTree(organization);
        return list;
    }

    
    public List getJustList() throws Exception{
    	OrganizationManager organizationManager=OrganizationManager.getInstance();
    	List list = organizationManager.justList(new Organization());
        return list;
    }
    
    public String getChildrenNodeId(String id) throws Exception{
        Organization organization = new Organization(); 
        organization.setId(id);
        OrganizationManager organizationManager=OrganizationManager.getInstance();
        String str=organizationManager.getChildereNodeId(organization);
        return str;
    }
    
    public Organization findOrgByName(String name) throws Exception{
    	 Organization organization = new Organization();
    	 organization.setOrganizationName(name);
    	 OrganizationManager organizationManager=OrganizationManager.getInstance();
    	 organization=organizationManager.findByName(organization);
         return organization;
    }
    
    public static void main(String[] args) throws Exception{
        OrganizationService organizationService = new OrganizationService();
        Organization organization = new Organization();
        String str = organizationService.getChildrenNodeId("0");
        //organizationService.tolist(organization,0,10);
       System.out.println( "6666666666666  "+str);
    }
}
